<script setup lang="ts">
import {Search} from "@element-plus/icons-vue";
import * as DocApi from '@/api/wms/doc';
import {DICT_TYPE} from "@/utils/dict";

const docId = defineModel("docId");
const docNo = defineModel("docNo");

const props = defineProps({
  docType: {
    type: String,
    default: undefined
  },
  isGenNo: {
    type: Boolean,
    default: false
  },
  disabled: {
    type: Boolean,
    default: false
  }
});

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref("选择单据类型")


const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const docName = ref(undefined) //当前选中的单据名称
const queryFormRef = ref() // 搜索的表单
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  code: undefined,
  docType: props.docType,
  status: 0
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DocApi.getDocPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 展示弹出框 */
const openDialog = () => {
  if (props.disabled) {
    return;
  }
  dialogVisible.value = true;
  getList();
}

/** 关闭弹出框 */
const closeDialog = () => {
  queryFormRef.value.resetFields(); //重置表单
  dialogVisible.value = false; //关闭选择开窗
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields();
  handleQuery();
}
/** 选择操作 */
const selected = (row) => {
  docId.value = row.id;
  docName.value = row.name;
  //后台自动生成单号
  if (props.isGenNo) {
    DocApi.getDocNo(row.id).then(res => {
      docNo.value = res;
    });
  }
  closeDialog();
}
/** 行点击事件*/
const rowClick = (row, column, event) => {
  selected(row);
}

watch(docId, (newVal) => {
  if (newVal === null || newVal === undefined) {
    docName.value = undefined;
    docNo.value = undefined;
  }else {
    DocApi.getDoc(newVal).then(response => {
      docName.value = response.name;
    });
  }
}, {immediate: true})
</script>

<template>
  <!-- 输入框 -->
  <el-input readonly v-model="docName"  placeholder="请选择单据类型" :disabled="disabled">
    <template #append>
      <el-button :icon="Search" @click="openDialog()" />
    </template>
  </el-input>
  <!-- 弹窗 -->
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="60%">
    <!-- 搜索工作栏 -->
    <div class="mb-30px">
      <el-form
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="单据名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入单据名称"
            @keyup.enter="handleQuery"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="单据编号" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入单据编号"
            @keyup.enter="handleQuery"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="content">
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" border max-height="500" @row-dblclick="rowClick">
        <el-table-column label="单据编号" align="center" prop="code" />
        <el-table-column label="单据名称" align="center" prop="name" />
        <el-table-column label="单据性质" align="center" prop="docType">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.DOC_WMS" :value="scope.row.docType" />
          </template>
        </el-table-column>
        <el-table-column label="交易类型" align="center" prop="transactionType">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.WMS_TRANSACTION_TYPE" :value="scope.row.transactionType" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="selected(scope.row)"
            >
              选择
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

  </Dialog>
</template>

<style scoped lang="scss">
.content {
  overflow: hidden;
}
</style>
